/*     */ package com.zimbra.cs.datasource;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.account.DataSource.DataImport;
/*     */ import com.zimbra.cs.filter.RuleManager;
/*     */ import com.zimbra.cs.mailbox.DeliveryContext;
/*     */ import com.zimbra.cs.mailbox.DeliveryOptions;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.soap.type.DataSource.ConnectionType;
/*     */ import java.io.IOException;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class MailItemImport
/*     */   implements DataSource.DataImport
/*     */ {
/*     */   protected final DataSource dataSource;
/*     */   protected final Mailbox mbox;
/*     */   
/*     */   public MailItemImport(DataSource ds)
/*     */     throws ServiceException
/*     */   {
/*  42 */     this.dataSource = ds;
/*  43 */     this.mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */   }
/*     */   
/*     */   public void validateDataSource() throws ServiceException {
/*  47 */     DataSource ds = getDataSource();
/*  48 */     if (ds.getHost() == null) {
/*  49 */       throw ServiceException.FAILURE(ds + ": host not set", null);
/*     */     }
/*  51 */     if (ds.getPort() == null) {
/*  52 */       throw ServiceException.FAILURE(ds + ": port not set", null);
/*     */     }
/*  54 */     if (ds.getConnectionType() == null) {
/*  55 */       throw ServiceException.FAILURE(ds + ": connectionType not set", null);
/*     */     }
/*  57 */     if (ds.getUsername() == null) {
/*  58 */       throw ServiceException.FAILURE(ds + ": username not set", null);
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean isOffline() {
/*  63 */     return getDataSource().isOffline();
/*     */   }
/*     */   
/*     */ 
/*     */   public Message addMessage(OperationContext octxt, ParsedMessage pm, int size, int folderId, int flags, DeliveryContext dc)
/*     */     throws ServiceException, IOException
/*     */   {
/*  70 */     Message msg = null;
/*  71 */     switch (folderId) {
/*     */     case 2: 
/*     */       try {
/*  74 */         List<ItemId> addedMessageIds = RuleManager.applyRulesToIncomingMessage(octxt, this.mbox, pm, size, this.dataSource.getEmailAddress(), dc, 2, true);
/*     */         
/*  76 */         Integer newMessageId = getFirstLocalId(addedMessageIds);
/*  77 */         if (newMessageId == null) {
/*  78 */           return null;
/*     */         }
/*  80 */         msg = this.mbox.getMessageById(null, newMessageId.intValue());
/*     */         
/*     */ 
/*  83 */         if (flags != Flag.BITMASK_UNREAD)
/*     */         {
/*  85 */           flags &= (Flag.BITMASK_DRAFT ^ 0xFFFFFFFF);
/*  86 */           this.mbox.setTags(octxt, newMessageId.intValue(), MailItem.Type.MESSAGE, flags, MailItem.TAG_UNCHANGED);
/*     */         }
/*     */       } catch (Exception e) {
/*  89 */         ZimbraLog.datasource.warn("Error applying filter rules", e);
/*     */       }
/*     */     
/*     */     case 5: 
/*     */     case 6: 
/*  94 */       flags |= Flag.BITMASK_FROM_ME;
/*     */     }
/*     */     
/*  97 */     if (msg == null) {
/*  98 */       msg = this.mbox.addMessage(octxt, pm, new DeliveryOptions().setFolderId(folderId).setFlags(flags), null);
/*     */     }
/* 100 */     return msg;
/*     */   }
/*     */   
/*     */   public boolean isSslEnabled() {
/* 104 */     return this.dataSource.getConnectionType() == DataSource.ConnectionType.ssl;
/*     */   }
/*     */   
/*     */   public DataSource getDataSource() {
/* 108 */     return this.dataSource;
/*     */   }
/*     */   
/*     */   public Mailbox getMailbox() {
/* 112 */     return this.mbox;
/*     */   }
/*     */   
/*     */   public Integer getFirstLocalId(List<ItemId> idList) {
/* 116 */     if (idList == null) {
/* 117 */       return null;
/*     */     }
/* 119 */     for (ItemId id : idList) {
/* 120 */       if (id.belongsTo(this.mbox)) {
/* 121 */         return Integer.valueOf(id.getId());
/*     */       }
/*     */     }
/* 124 */     return null;
/*     */   }
/*     */   
/*     */   public void checkIsEnabled() throws ServiceException {
/* 128 */     if (!getDataSource().isManaged()) {
/* 129 */       throw ServiceException.FAILURE("Import aborted because data source has been deleted or disabled", null);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/datasource/MailItemImport.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */